﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_HealingWave : TargetAreaSpell
    {
        public Spell_HealingWave() : base() { }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;


            MapItem targetsMapItem = null;
            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    targetsMapItem = mapitem;

                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            targetsMapItem.SpellLayer1.Add(this);
            MapItems.Add(targetsMapItem);

            foreach (CreatureBase creaturebase in targetsMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction)
                {
                    foreach (Buff currentBuff in creaturebase.buffManager.Buffs)
                    {
                        if (currentBuff.spellIcon.Name == this.spellIcon.Name)
                        {
                            //Remove the spell but dont update the buffs or send messages
                            //since we are actually just replacing it
                            currentBuff.RemoveSpell(false, false);
                            break;
                        }
                    }

                    //We will probably replace this with the resume function (Not yet built for HealingWave)
                    Spell_HealingWaveBuff buff = new Spell_HealingWaveBuff();
                    CreatureBase cb = creaturebase;
                    buff.Resume(cb, this.spellIcon, 40, CastLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                    cb.buffManager.UpdateBuffs();

                    if (myCreatureBase is Character)
                    {
                        Character character = (Character)myCreatureBase;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelldefense";
                        msg.Text = "You cast " + this.spellIcon.Name + " on " + creaturebase.Name;

                        character.AddMessage(msg);
                    }
                    if (creaturebase is Character)
                    {
                        Character character = (Character)creaturebase;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelldefense";
                        msg.Text = myCreatureBase.Name + " casts " + this.spellIcon.Name + " on you";

                        character.AddMessage(msg);
                    }
                }
            }

            foreach (Character character in CharactersInUpdateRange)
            {
                //Play Sound

                if (Math.Abs(character.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(character.MapY - TargetCreatureBase.MapY) <= 4)
                    character.AddMapItem(targetsMapItem);
            }

        }
    }
}
